// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package edu.uoc.tfc.dbmodel.security;

import org.apache.commons.lang.StringUtils;



/**
 * @see edu.uoc.tfc.dbmodel.security.User
 */
public class UserDaoImpl
    extends edu.uoc.tfc.dbmodel.security.UserDaoBase
{
    /**
     * @see edu.uoc.tfc.dbmodel.security.UserDao#toUserVO(edu.uoc.tfc.dbmodel.security.User, edu.uoc.tfc.dto.security.UserVO)
     */
    public void toUserVO(
        edu.uoc.tfc.dbmodel.security.User source,
        edu.uoc.tfc.dto.security.UserVO target)
    {
        // @todo verify behavior of toUserVO
        super.toUserVO(source, target);
    }


    /**
     * @see edu.uoc.tfc.dbmodel.security.UserDao#toUserVO(edu.uoc.tfc.dbmodel.security.User)
     */
    public edu.uoc.tfc.dto.security.UserVO toUserVO(final edu.uoc.tfc.dbmodel.security.User entity)
    {
        // @todo verify behavior of toUserVO
        return super.toUserVO(entity);
    }


    /**
     * Retrieves the entity object that is associated with the specified value object
     * from the object store. If no such entity object exists in the object store,
     * a new, blank entity is created
     */
    private edu.uoc.tfc.dbmodel.security.User loadUserFromUserVO(edu.uoc.tfc.dto.security.UserVO userVO)
    {
        //A typical implementation looks like this:
        edu.uoc.tfc.dbmodel.security.User user = this.load(Long.valueOf(userVO.getId()));
        if (user == null)
        {
            user = edu.uoc.tfc.dbmodel.security.User.Factory.newInstance();
        }
        return user;
        
    }

    
    /**
     * @see edu.uoc.tfc.dbmodel.security.UserDao#userVOToEntity(edu.uoc.tfc.dto.security.UserVO)
     */
    public edu.uoc.tfc.dbmodel.security.User userVOToEntity(edu.uoc.tfc.dto.security.UserVO userVO)
    {
        // @todo verify behavior of userVOToEntity
        edu.uoc.tfc.dbmodel.security.User entity = this.loadUserFromUserVO(userVO);
        Role rol = (Role)this.getHibernateTemplate().find
        		("FROM edu.uoc.tfc.dbmodel.security.Role WHERE id = " + 
        				userVO.getRolVO().getId().toString()).get(0);
        entity.setRol(rol);
        this.userVOToEntity(userVO, entity, true);
        return entity;
    }


    /**
     * @see edu.uoc.tfc.dbmodel.security.UserDao#userVOToEntity(edu.uoc.tfc.dto.security.UserVO, edu.uoc.tfc.dbmodel.security.User)
     */
    public void userVOToEntity(
        edu.uoc.tfc.dto.security.UserVO source,
        edu.uoc.tfc.dbmodel.security.User target,
        boolean copyIfNull)
    {
        // @todo verify behavior of userVOToEntity
        super.userVOToEntity(source, target, copyIfNull);
        
    }
    
    public java.util.Collection findUsersByCriteria(edu.uoc.tfc.dto.security.CriteriaUserVO criteriaUser)
    {
        return this.findUsersByCriteria(TRANSFORM_NONE, criteriaUser);
    }

    public java.util.Collection findUsersByCriteria(final int transform, final edu.uoc.tfc.dto.security.CriteriaUserVO criteriaUser)
    {
        return this.findUsersByCriteria(transform, -1, -1, criteriaUser);
    }
    
    public java.util.Collection findUsersByCriteria(final int transform, final int pageNumber, final int pageSize, final edu.uoc.tfc.dto.security.CriteriaUserVO criteriaUser)
    {
        try
        {
            edu.uoc.tfc.CriteriaSearch criteriaSearch = new edu.uoc.tfc.CriteriaSearch(super.getSession(false), edu.uoc.tfc.dbmodel.security.UserImpl.class);
            criteriaSearch.getConfiguration().setFirstResult(criteriaUser.getFirstResult());
            criteriaSearch.getConfiguration().setFetchSize(criteriaUser.getFetchSize());
            criteriaSearch.getConfiguration().setMaximumResultSize(criteriaUser.getMaximumResultSize());
            if (pageNumber > 0 && pageSize > 0)
            {
                criteriaSearch.getConfiguration().setFirstResult(new java.lang.Integer(calculateFirstResult(pageNumber, pageSize)));
                criteriaSearch.getConfiguration().setMaximumResultSize(new java.lang.Integer(pageSize));
            }
            edu.uoc.tfc.CriteriaSearchParameter parameter1 =
                new edu.uoc.tfc.CriteriaSearchParameter(
                    criteriaUser.getUserId(),
                    "userId", org.hibernate.criterion.MatchMode.ANYWHERE);
            criteriaSearch.addParameter(parameter1);
            edu.uoc.tfc.CriteriaSearchParameter parameter2 =
                new edu.uoc.tfc.CriteriaSearchParameter(
                    criteriaUser.getNom(),
                    "nom", org.hibernate.criterion.MatchMode.ANYWHERE);
            criteriaSearch.addParameter(parameter2);
            
            if(StringUtils.isNotEmpty(criteriaUser.getRol())){
	            edu.uoc.tfc.CriteriaSearchParameter parameter3 =
	                    new edu.uoc.tfc.CriteriaSearchParameter(
	                    	getHibernateTemplate().load(RoleImpl.class, 
	                    			Long.valueOf(criteriaUser.getRol())),
	                    				"rol");
	            criteriaSearch.addParameter(parameter3);
            }
            
            java.util.List results = criteriaSearch.executeAsList();
            transformEntities(transform, results);
            return results;
        }
        catch (org.hibernate.HibernateException ex)
        {
            throw super.convertHibernateAccessException(ex);
        }
    }    

}